package com.tanzeer.deliverytracker.dbhelper;

import java.util.ArrayList;
import java.util.HashMap;

import com.tanzeer.deliverytracker.models.DelivaryTracks;
import com.tanzeer.deliverytracker.models.RowItem;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBOpenHelper extends SQLiteOpenHelper {

	private static String Databasename = "DELIVERY";
	private static int Databasevertion = 1;
	private String deleivarytracks = "deliverytracks";

	public DBOpenHelper(Context context) {
		super(context, Databasename, null, Databasevertion);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		String sql = "create table " + deleivarytracks + "(" + "_id "
				+ "INTEGER PRIMARY KEY AUTOINCREMENT," + " track_number "
				+ " TEXT, " + " description " + " TEXT, " + "category "
				+ " TEXT, " + " status " + " TEXT" + ")";
		db.execSQL(sql);

	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub

	}

	public ArrayList<DelivaryTracks> getAllList() {
		ArrayList<DelivaryTracks> list = new ArrayList<DelivaryTracks>();
		SQLiteDatabase db = this.getReadableDatabase();
		String[] colums = { "track_number", "description", "category", "_id",
				"status" };
		Cursor c = db.query(deleivarytracks, colums, null, null, null, null,
				null);
		while (c.moveToNext()) {
			String status = c.getString(4);
			if (status.equals("0")) {
				list.add(new DelivaryTracks(c.getString(0), c.getString(1), c
						.getString(2), c.getShort(3)));
			}
		}
		c.close();
		db.close();
		return list;
	}

	public ArrayList<DelivaryTracks> getAllCompleteList() {
		ArrayList<DelivaryTracks> list = new ArrayList<DelivaryTracks>();
		SQLiteDatabase db = this.getReadableDatabase();
		String[] colums = { "track_number", "description", "category", "_id",
				"status" };
		Cursor c = db.query(deleivarytracks, colums, null, null, null, null,
				null);
		while (c.moveToNext()) {
			String status = c.getString(4);
			if (status.equals("1")) {
				list.add(new DelivaryTracks(c.getString(0), c.getString(1), c
						.getString(2), c.getShort(3)));
			}
		}
		c.close();
		db.close();
		return list;
	}

	public ArrayList<String> getActiveCategoryList() {
		ArrayList<String> list = new ArrayList<String>();
		SQLiteDatabase db = this.getReadableDatabase();
		String[] colums = { "category", "status" };
		Cursor c = db.query(deleivarytracks, colums, null, null, null, null,
				null);
		while (c.moveToNext()) {
			String status = c.getString(1);
			if (status.equals("0")) {
				String cat = c.getString(0);
				if (!list.contains(cat))
					list.add(cat);
			}
		}
		c.close();
		db.close();
		return list;
	}

	public ArrayList<DelivaryTracks> getActiveCategoryListByType(String str) {
		ArrayList<DelivaryTracks> list = new ArrayList<DelivaryTracks>();
		SQLiteDatabase db = this.getReadableDatabase();
		String[] colums = { "track_number", "description", "category", "_id",
				"status" };
		Cursor c = db.query(deleivarytracks, colums, null, null, null, null,
				null);
		while (c.moveToNext()) {
			String status = c.getString(4);
			if (status.equals("0")) {
				if (c.getString(2).equals(str))
					list.add(new DelivaryTracks(c.getString(0), c.getString(1),
							c.getString(2), c.getShort(3)));
			}
		}
		c.close();
		db.close();
		return list;
	}

	public void saveDelivary(String tid, String des, String type) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("track_number", tid);
		values.put("description", des);
		values.put("category", type);
		values.put("status", "0");
		db.insert(deleivarytracks, null, values);
		db.close();
	}

	public void updateDelivary(String tid, String des, String type, int id) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("track_number", tid);
		values.put("description", des);
		values.put("category", type);
		Log.e("id", "" + id);
		db.update(deleivarytracks, values, "_id " + " = " + id, null);
		db.close();
	}

	public void removeDelivary(int id) {
		SQLiteDatabase db = this.getWritableDatabase();
		// ContentValues values=new ContentValues();
		db.delete(deleivarytracks, "_id " + " = " + id, null);
		db.close();
	}

	public void updateDelivaryasMarked(int id) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		// values.put("track_number",tid);
		// values.put("description",des);
		// values.put("category",type);
		values.put("status", "1");
		db.update(deleivarytracks, values, "_id " + " = " + id, null);
		db.close();
	}
	public void updateDelivaryasPending(int id) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		// values.put("track_number",tid);
		// values.put("description",des);
		// values.put("category",type);
		values.put("status", "0");
		db.update(deleivarytracks, values, "_id " + " = " + id, null);
		db.close();
	}
	
	

}
